// pages/card-detail/index.js
const app = getApp();
const { getBalanceLog, getIntegralLog } = require('../../api/member');
const { PageBase } = require('../../mixins/page');

Page({
    mixins: [PageBase],
    data: {
        activeTab: 0,
        cardInfo: null,
        tabList: [{
            title: '详情信息',
        }, {
            title: '余额变动',
        }, {
            title: '积分变动',
        }],
        balanceLogList: [],
        integralLogList: [],
        pageSize: 10,
        balancePageNumber: 1,
        integralPageNumber: 1,
    },
    onLoad: function (options) {
        const { tab } = options;
        let idx = 0;
        console.log(tab);
        if (tab == 'balance') idx = 1;
        if (tab == 'integral') idx = 2;
        this.setData({
            activeTab: idx,
            cardInfo: app.globalData.cardInfo,
        });
    },
    async handleBalanceLog() {
        const { pageSize } = this.data;
        const param = {
            page_no: 1,
            page_size: pageSize,
        };
        const list = await getBalanceLog(param);
        console.log('CardDetail => 获取卡余额变动记录', list);
        this.setData({
            balanceLogList: list,
            balancePageNumber: 1,
        });
    },
    async handleBalanceLogNext() {
        const { pageSize, balancePageNumber, balanceLogList } = this.data;
        const pageNumber = balancePageNumber + 1;
        const param = {
            page_no: pageNumber,
            page_size: pageSize,
        };
        const list = await getBalanceLog(param);
        this.setData({
            balanceLogList: [...balanceLogList, ...list],
            balancePageNumber: pageNumber,
        });
    },
    async handleIntegralLog() {
        const { pageSize } = this.data;
        const param = {
            page_no: 1,
            page_size: pageSize,
        };
        const list = await getIntegralLog(param);
        this.setData({
            integralLogList: list,
            integralPageNumber: 1,
        });
    },
    async handleIntegralLogNext() {
        const { pageSize, integralPageNumber, integralLogList } = this.data;
        const pageNumber = integralPageNumber + 1;
        const param = {
            page_no: pageNumber,
            page_size: pageSize,
        };
        const list = await getIntegralLog(param);
        this.setData({
            integralLogList: [...integralLogList, ...list],
            integralPageNumber: pageNumber,
        });
    },
    
    async changeTabAction(event) {
        const idx = event.detail.index;
        if (idx === 1) {
            await this.handleBalanceLog();
        }
        if (idx === 2) {
            await this.handleIntegralLog();
        }
    },
})